package com.wswy.wyjk.api;

import android.app.AlertDialog;
import android.content.Context;

import com.my.httpapi.api.baseApi.ComObserver;
import com.my.httpapi.api.baseApi.ComTransformer;
import com.my.httpapi.api.baseApiInterface.OnLoadingListener;
import com.my.httpapi.api.baseApiInterface.ProgressManageError;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;

public class BaseController {
    protected CompositeDisposable mCompositeDisposable;
    protected Context context;

    public BaseController(Context context) {
        this.context = context;
        if (mCompositeDisposable == null) mCompositeDisposable = new CompositeDisposable();
    }

    protected void destory() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }
    }

    protected void subscribe(Observable observable, ComObserver comObserver) {
        observable.compose(new ComTransformer()).subscribe(comObserver);
    }

    /**
     * @显示默认loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener) {
        return request(onLoadingListener, true);
    }

    /**
     * @自定义是否显示loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, context, isLoading);
    }

    /**
     * @自定义loading样式的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, AlertDialog alertDialog) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, context, true);
    }

    /**
     * @显示默认loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError) {
        return request(onLoadingListener, progressManageError, true);
    }

    /**
     * @自定义是否显示loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, progressManageError, context, isLoading);
    }

    /**
     * @自定义loading样式的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, AlertDialog alertDialog, ProgressManageError progressManageError) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, progressManageError, context, true);
    }
}
